package samsung.sds.survey.surveyVote.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.survey.surveyVote.dao.VoteQuestionMapper;
import samsung.sds.survey.surveyVote.model.QuestionEntity;
import samsung.sds.survey.surveyVote.model.QuestionOptionDTO;
import samsung.sds.survey.surveyVote.service.VoteQuestionOptionService;
import samsung.sds.survey.surveyVote.service.VoteQuestionService;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.system.common.model.QueryParam;

/**
 * 
 * 获取Question的service
 * 
 * @author lixin.qiu
 * @createDate 2016-01-18
 */
@Service
public class VoteQuestionServiceImpl extends AbstractBaseService<QuestionEntity>
		implements VoteQuestionService {

	@Autowired
	private VoteQuestionMapper questionMapper;

	@Autowired
	private VoteQuestionOptionService voteQuestionOptionServiceImpl;

	@Override
	protected BaseMapper<QuestionEntity> getMapper() {
		return questionMapper;
	}

	/**
	 * 根据surveyId查询问题列表
	 * 
	 * @param surveyId
	 * @return
	 */
	@Override
	public List<QuestionEntity> queryList(QueryParam param) {
		List<QuestionEntity> qt = questionMapper.queryList(param);
		for (int i = 0; i < qt.size(); i++) {
			param.getQueryCondition().put("questionId", qt.get(i).getId());
			List<QuestionOptionDTO> qos = voteQuestionOptionServiceImpl
					.queryList(param);
			qt.get(i).setQuestionOptionList(qos);
			// 如果是数字类型问题，获取约束规则的最大值最小值
			if ("5".equals(qt.get(i).getQuestionType())) {
				String[] rule = qt.get(i).getValidateRule().split(",");
				qt.get(i).setMinVal(rule[0]);
				qt.get(i).setMaxVal(rule[1]);
			}
			// YN型问题选项
			if ("6".equals(qt.get(i).getQuestionType())) {
				List<QuestionOptionDTO> questionOptionList = new ArrayList<QuestionOptionDTO>();
				QuestionOptionDTO questionOption = new QuestionOptionDTO();
				questionOption.setOptionValue("1");
				questionOption.setOptionName("Y");
				/*if(qt.get(i).getValidateRule().equals("Y")){
					questionOption.setCheckValue(true);
				}*/
				questionOptionList.add(questionOption);
				QuestionOptionDTO questionOptionNo = new QuestionOptionDTO();
				questionOptionNo.setOptionValue("0");
				questionOptionNo.setOptionName("N");
				/*if(qt.get(i).getValidateRule().equals("N")){
					questionOptionNo.setCheckValue(true);
				}*/
				questionOptionList.add(questionOptionNo);
				qt.get(i).setQuestionOptionList(questionOptionList);
			}
		}
		return qt;
	}

	@Override
	public List<QuestionEntity> queryListWithDefault(QueryParam param) {
		List<QuestionEntity> qt = questionMapper.queryList(param);
		for (int i = 0; i < qt.size(); i++) {
			param.getQueryCondition().put("questionId", qt.get(i).getId());
			List<QuestionOptionDTO> qos = voteQuestionOptionServiceImpl
					.queryList(param);
			qt.get(i).setQuestionOptionList(qos);
			// 如果是数字类型问题，获取约束规则的最大值最小值
			if ("5".equals(qt.get(i).getQuestionType())) {
				String[] rule = qt.get(i).getValidateRule().split(",");
				qt.get(i).setMinVal(rule[0]);
				qt.get(i).setMaxVal(rule[1]);
			}
			// YN型问题选项
			if ("6".equals(qt.get(i).getQuestionType())) {
				List<QuestionOptionDTO> questionOptionList = new ArrayList<QuestionOptionDTO>();
				QuestionOptionDTO questionOption = new QuestionOptionDTO();
				questionOption.setOptionValue("1");
				questionOption.setOptionName("Y");
				if(qt.get(i).getValidateRule().equals("Y")){
					questionOption.setCheckValue(true);
				}
				questionOptionList.add(questionOption);
				QuestionOptionDTO questionOptionNo = new QuestionOptionDTO();
				questionOptionNo.setOptionValue("0");
				questionOptionNo.setOptionName("N");
				if(qt.get(i).getValidateRule().equals("N")){
					questionOptionNo.setCheckValue(true);
				}
				questionOptionList.add(questionOptionNo);
				qt.get(i).setQuestionOptionList(questionOptionList);
			}
		}
		return qt;
	}
}
